<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="告警温度(℃)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'alarmTemperature', validatorRules.alarmTemperature]" placeholder="请输入告警温度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="告警湿度最大值(RH)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'alarmHumidity', validatorRules.alarmHumidity]" :max=999 :min=50 placeholder="请输入告警最大湿度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="告警湿度最小值(RH)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'alarmMinHumidity', validatorRules.alarmMinHumidity]" :max=50 :min=1 placeholder="请输入告警最小湿度" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="告警PM25(ug/m³)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'alarmPm25', validatorRules.alarmPm25]" placeholder="请输入告警PM25" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="告警CO2(ppm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'alarmCo2', validatorRules.alarmCo2]" placeholder="请输入告警CO2" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="告警Voc(ppm)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'alarmVoc', validatorRules.alarmVoc]" placeholder="请输入告警voc" style="width: 100%"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  export default {
    name: "GlrSensorSetModal",
    components: {
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          alarmTemperature: {rules: [{required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          alarmHumidity: {rules: [
                  {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          alarmPm25: {rules: [
                  {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          alarmCo2: {rules: [
                  {required: true, message: this.$t('base.pleaseEnter')+'!'},
          ]},
          alarmMinHumidity: {rules: [
                  {required: true, message: this.$t('base.pleaseEnter')+'!'},
              ]},
        },
        url: {
          add: "/glr/glrSensorSet/add",
          edit: "/glr/glrSensorSet/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'alarmTemperature','alarmHumidity','alarmPm25','alarmCo2','sysCompanyCode','alarmMinHumidity','alarmVoc'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'alarmTemperature','alarmHumidity','alarmPm25','alarmCo2','sysCompanyCode','alarmVoc'))
      },


    }
  }
</script>